#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
 pid_t id = fork();
 if(id == 0)
 {
   //int cnt = 5;
   //child
   while(1)
   {
     printf("我是子进程，我正在运行.... Pid:%d\n",getpid());
     sleep(3);
     //break;
     //除零异常
     //int a  = 10/0;
     //cnt--;
     //if(!cnt)
     //{
     //  break;
     //}
   }
   exit(0);
 }
 else
 {
  //parent
  int status = 0;
  while(1)
  {
    pid_t ret = waitpid(-1,&status,WNOHANG);
    if(ret>0)
    {
      printf("等待成功，%d,exit sig:%d,exit code:%d\n",
          ret,status&0x7F,(status>>8)&0xFF);

    }
    else if(ret == 0)
    {
      //等待成功了 但是子进程没有退出
      printf("子进程好了吗,还没，那么父进程做其他事情.....\n");
      sleep(1);
    }
    else{
      //出错了 暂时不处理
    }
  }
  /* 
  int status = 0;
  printf("我是父进程，pid：%d,我准备等待子进程啦\n",getpid());
  pid_t ret = waitpid(id,&status,0);
  if(ret > 0)
  {
    //是否正产退出
    if(WIFEXITED(status))
    {
      printf("子进程是正常退出的，退出码：%d\n",WEXITSTATUS(status));
    }
    //printf("wait success,ret : %d,我所等待的子进程的退出码:%d,退出信号是：%d\n",
    //    ret,(status>>8)&0xFF,status&0x7F);
  }
  */
  /*
  sleep(40);
  pid_t ret = wait(NULL);
  if(ret<0)
  {
    printf("等待失败\n");
  }
  else
  {
    printf("等待成功: result: %d\n",ret);
  }
  sleep(20);
  */
 }
}
